// pages/operation/classRecord/classRecordInfo/index.js
import { timestampToTime } from "../../../../utils/index.js"
import { ClassModel } from "../../../../api/api-work/classModel.js"
import formRules from "../../../../utils/rules-form.js";
import Toast from '@vant/weapp/toast/toast';
const classModel = new ClassModel();

const rules = {
  classDate: [{
    required: true,
    message: "请选择上课日期"
  }],
  classStartTime: [{
    required: true,
    message: "请选择上课时间"
  }],
  classEndTime: [{
    required: true,
    message: "请选择下课时间"
  }],
  teacherName: [{
    required: true,
    message: "请选择主班老师"
  }],
  classRoomName: [{
    required: true,
    message: "请选择上课教室"
  }]
}
Page({

  /**
   * 页面的初始数据
   */
  data: {
    className: "",
    classHour: "",
    courseId: "",
    courseName: "", // 授课课程
    // 上课日期
    showClassDate: false,
    classDate: "请选择", // 上课日期
    currentDate: new Date().getTime(), // 当前选中的日期
    minDate: new Date(2020, 0, 1).getTime(),
    // 上课时间
    classStartTime: "", // 上课时间
    showStartTime: false,
    filterStartTime(type, options) {
      if (type === 'minute') {
        return options.filter((option) => option % 5 === 0);
      }
      return options;
    },
    // 下课时间
    classEndTime: "",
    showEndTime: false,
    filterEndTime(type, options) {
      if (type === 'minute') {
        return options.filter((option) => option % 5 === 0);
      }
      return options;
    },
    // 主班老师
    showTeacher: false,
    teacherName: "请选择",
    teacher: "", // 主班老师id
    teacherList: [], // 老师列表
    // 助教老师
    showAssistant: false,
    assistantName: "请选择", // 助教老师
    assistant: "",
    assistantArr: [],
    assistantList: [], // 助教老师列表
    // 上课教室
    classRoomName: "请选择", // 上课教室
    classRoom: "", // 上课教室id
    showRoom: false,
    roomList: [],
    description: "", // 上课内容
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    const param = JSON.parse(options.param)
    console.log("填写补课信息: ", param)
    this.setData({
      classId: param.classId,
      courseId: param.courseId,
      courseName: param.courseName,
      classHour: param.classHour,
      selectStudents: param.selectStudents,
      className: param.className
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  // 切换日期
  showDatePopup() {
    this.setData({
      showClassDate: true
    })
  },
  // 关闭日期
  onCloseClassDate() {
    this.setData({
      showClassDate: false
    })
  },
  // 选择上课日期
  chooseClassDate(e) {
    this.setData({
      classDate: timestampToTime(e.detail),
      currentDate: new Date(timestampToTime(e.detail)).getTime(),
      showClassDate: false
    })
  },

  // 上课时间
  showStartTimePopup() {
    this.setData({
      showStartTime: true
    })
  },
  //
  onCloseStartTime() {
    this.setData({
      showStartTime: false
    })
  },
  // 选择上课时间
  chooseStartTime(e) {
    const date = new Date();
    const start_time = e.detail.split(":");
    const end_time = this.data.classEndTime.split(":");
    if (date.setHours(start_time[0], start_time[1]) > date.setHours(end_time[0], end_time[1])) {
      Toast('上课时间不能大于下课时间');
      return;
    }
    console.log("sss: ", e)
    this.setData({
      showStartTime: false,
      classStartTime: e.detail
    })
  },

  // 下课时间
  showEndTimePopup() {
    this.setData({
      showEndTime: true
    })
  },
  // 选择下课时间
  chooseEndTime(e) {
    const date = new Date();
    const start_time = this.data.classStartTime.split(":");
    const end_time = e.detail.split(":");
    if (date.setHours(start_time[0], start_time[1]) > date.setHours(end_time[0], end_time[1])) {
      Toast('下课时间不能小于上课时间');
      return;
    }
    this.setData({
      showEndTime: false,
      classEndTime: e.detail
    })
  },
  onCloseEndTime() {
    this.setData({
      showEndTime: false
    })
  },

  // 选择主班老师
  showTeacherPopup() {
    classModel.getStaffPeopleListApi().then(res => {
      if (res.length > 0) {
        res.forEach(item => {
          if (item.value == this.data.teacher) {
            item['isChecked'] = true
          }
          else {
            item['isChecked'] = false;
          }
        })
      }
      this.setData({
        teacherList: res,
        showTeacher: true
      })
    })
  },
  // 选择主班老师  单选
  onCheckTeacher(e) {
    this.data.teacherList.forEach(item => {
      if (item.value == e.currentTarget.dataset.query.value) {
        item.isChecked = true
      }
      else {
        item.isChecked = false
      }
    })
    this.setData({
      teacherList: this.data.teacherList
    })
  },
  chooseTeacher() {
    this.data.teacherList.forEach(item => {
      if (item.isChecked) {
        this.setData({
          showTeacher: false,
          teacherName: item.label,
          teacher: item.value
        })
      }
    })
  },
  onCloseTeacher() {
    this.setData({
      showTeacher: false
    })
  },

  showAssistantPopup() {
    classModel.getStaffPeopleListApi().then(res => {
      if (res.length > 0) {
        res.forEach(item => {
          if (this.data.assistantArr && this.data.assistantArr.indexOf(item.value) != -1) {
            item['isChecked'] = true
          }
          else {
            item['isChecked'] = false;
          }
        })
      }
      this.setData({
        assistantList: res,
        showAssistant: true
      })
    })
  },

  onCheckAssistant(e) {
    this.data.assistantList.forEach(item => {
      if (item.value == e.currentTarget.dataset.query.value) {
        item.isChecked = !item.isChecked
      }
    })
    this.setData({
      assistantList: this.data.assistantList
    })
  },
  chooseAssistant() {
    let arrIds = [];
    let arrLabels = [];
    this.data.assistantList.forEach(item => {
      if (item.isChecked) {
        arrIds.push(item.value);
        arrLabels.push(item.label)
      }
    })
    this.setData({
      showAssistant: false,
      assistantName: arrLabels.join(","),
      assistant: arrIds.join(","),
      assistantArr: arrIds
    })
  },
  onCloseAssistant() {
    this.setData({
      showAssistant: false
    })
  },

  showRoomPopup() {
    classModel.getClassRoomListApi({ lableType: "class_room" }).then(res => {
      if (res.length > 0) {
        res.forEach(item => {
          if (item.id == this.data.classRoom) {
            item['isChecked'] = true
          }
          else {
            item['isChecked'] = false
          }
        })
      }
      this.setData({
        showRoom: true,
        roomList: res
      })
    })
  },

  onCheckRoom(e) {
    this.data.roomList.forEach(item => {
      if (item.id == e.currentTarget.dataset.query.id) {
        item.isChecked = true
      }
      else {
        item.isChecked = false
      }
    })
    this.setData({
      roomList: this.data.roomList
    })
  },

  chooseRoom() {
    this.data.roomList.forEach(item => {
      if (item.isChecked) {
        this.setData({
          showRoom: false,
          classRoom: item.id,
          classRoomName: item.name
        })
      }
    })
  },

  onCloseRoom() {
    this.setData({
      showRoom: false
    })
  },

  // 去点名
  jumpToOrder() {

    formRules(rules, this.data, (status) => {
      if (status) {
        console.log("校验成功")
        const obj = {
          courseId: this.data.courseId,
          courseName: this.data.courseName,
          className: this.data.className,
          classDate: this.data.classDate,
          classStartTime: this.data.classStartTime,
          classEndTime: this.data.classEndTime,
          teacherName: this.data.teacherName,
          teacher: this.data.teacher,
          classRoomName: this.data.classRoomName,
          selectStudents: this.data.selectStudents,
          classHour: this.data.classHour,
          classId: this.data.classId
        }
        wx.navigateTo({
          url: `/pages/operation/class/classOrder/index?param=${JSON.stringify(obj)}`,
        })
      }
      else {
        console.log("校验失败")
      }
    })
    return;

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})